package com.wannengbang.cloudleader.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.StatisticsBean;
import com.wannengbang.cloudleader.performance.model.IPerformanceModel;
import com.wannengbang.cloudleader.performance.model.PerformanceModelImpl;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AgentPerformanceActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_order_fee_count_total)
    TextView tvOrderFeeCountTotal;
    @BindView(R.id.tv_look_details)
    TextView tvLookDetails;
    @BindView(R.id.tv_agent_count)
    TextView tvAgentCount;
    @BindView(R.id.ll_agent)
    LinearLayout llAgent;
    @BindView(R.id.tv_order_count)
    TextView tvOrderCount;
    @BindView(R.id.ll_order_count)
    LinearLayout llOrderCount;
    @BindView(R.id.tv_order_fee_count)
    TextView tvOrderFeeCount;
    @BindView(R.id.ll_transaction)
    LinearLayout llTransaction;
    @BindView(R.id.tv_device_activation_num)
    TextView tvDeviceActivationNum;
    @BindView(R.id.ll_device_activation_num)
    LinearLayout llDeviceActivationNum;
    @BindView(R.id.tv_total_device_num)
    TextView tvTotalDeviceNum;
    @BindView(R.id.tv_today_agent_count)
    TextView tvTodayAgentCount;
    @BindView(R.id.tv_standard_agent_count)
    TextView tvStandardAgentCount;
    @BindView(R.id.ll_standard_agent)
    LinearLayout llStandardAgent;
    private IPerformanceModel performanceModel;
    private String name;
    private String agent_id;
    private BottomSheetDialog selectPicDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_performance);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        initView();
    }

    public void initView() {
        name = getIntent().getStringExtra("name");
        agent_id = getIntent().getStringExtra("agent_id");
        performanceModel = new PerformanceModelImpl();
        tvName.setText(name);
        titleBar.setRightClickListener(() -> {
            showPictureSelectorDialog();
        });
        requestData();
    }

    public void requestData() {
        ViewLoading.showProgress(mActivity);
        performanceModel.requestChildLineStatistics(agent_id, "", "", new DataCallBack<StatisticsBean>() {
            @Override
            public void onSuccessful(StatisticsBean bean) {
                String orderFeeCountTotal = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getNew_order_sum()) / 100);
                tvOrderFeeCountTotal.setText(orderFeeCountTotal);
                tvAgentCount.setText(bean.getData().getNew_agent_count());
                tvOrderCount.setText(bean.getData().getNew_order_count());
                String orderFeeCount = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getNew_order_sum()) / 100);
                tvOrderFeeCount.setText(orderFeeCount);
                tvDeviceActivationNum.setText(bean.getData().getDevice_activation_num());
                tvTotalDeviceNum.setText(bean.getData().getDevice_sum());
                tvTodayAgentCount.setText(bean.getData().getNew_agent_count_today());
                tvStandardAgentCount.setText(bean.getData().getStandard_count());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void showPictureSelectorDialog() {
        selectPicDialog = new BottomSheetDialog(mActivity, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_bottom_select_pictrue, null);
        // 相册
        TextView album = view.findViewById(R.id.tv_select_pictrue_album);
        // 相机
        TextView camera = view.findViewById(R.id.tv_select_pictrue_camera);
        // 取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);
        album.setText("按日查询");
        camera.setText("按月查询");
        album.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity, AgentPerformanceQueryActivity.class);
            intent.putExtra("cycleType", "date");
            intent.putExtra("param", DateTimeUtil.getToDayDate2());
            intent.putExtra("agent_id", agent_id);
            intent.putExtra("name", name);
            startActivity(intent);
            cancelPictureDialog();
        });
        camera.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity, AgentPerformanceQueryActivity.class);
            intent.putExtra("cycleType", "month");
            intent.putExtra("param", DateTimeUtil.getMonthDate2());
            intent.putExtra("agent_id", agent_id);
            intent.putExtra("name", name);
            startActivity(intent);
            cancelPictureDialog();
        });
        cancel.setOnClickListener(v -> cancelPictureDialog());

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }

    private void cancelPictureDialog() {
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }
}
